<?php
namespace App\Http\Controllers\Api;

use App\V3\V3_1\Models\TallyClassModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Validator;

/**
 * 记账类型
 * Class TallyClassController
 * @package App\Http\Controllers\Api
 */
class TallyClassController extends BaseController
{
    /**
     * 列表
     * change 在 2021/10/26 10:59 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        $model = TallyClassModel::select('id','name')->where(['main_id'=>$this->getMainUserId(),'parent_id'=>0])->get();
        foreach ($model as &$value){
            $value['children'] = TallyClassModel::select('id','name','parent_id')->where('parent_id',$value['id'])->get();
        }
        return $this->success($model);
    }

    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' =>'required|string',//记账类型名称
            'parent_id' =>'required|integer',//父id 一级为0
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['main_id'] = $this->getMainUserId();
        $data['created_at'] = time();
        $id = TallyClassModel::insertGetId($data);
        $this->log('添加记账分类',$data);
        Log::debug('添加记账分类:'.var_export($data, true));
        return $this->success($id);
    }

    /**
     * 编辑
     * change 在 2021/10/26 10:59 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//记账分类主键id
//            'name' =>'required|string',//记账类型名称
//            'parent_id' =>'required|integer',//父id 一级为0
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        TallyClassModel::where('id',$data['id'])->update($data);
        $this->log('编辑记账类型',$data);
        return $this->success();
    }

    /**
     * 删除
     * change 在 2021/10/26 11:00 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){

        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required',//记账分类主键id  多条用逗号分隔
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = TallyClassModel::destroy(explode(',',$data['id']));
//        if($model['parent_id']==0){
//            $children = TallyClassModel::where(['main_id'=>$this->getMainUserId(),'parent_id'=>$model['id']])->first();
//            if($children){
//               return $this->fail('存在子类不能删除');
//            }
//        }
        return $this->success();
    }
}
